import win32gui, pyautogui, json
from pynput import mouse
import win32api

def find_window_by_title(window_title):
  """根据窗口标题寻找窗口句柄"""
  hwnd = win32gui.FindWindow(None, window_title)
  return hwnd

def get_screen_size_real():
    return pyautogui.size()

def get_screen_size_fake():
    return win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1)

def get_true_rect(hwnd):
    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    hDC = win32gui.GetDC(0)
    w, h = get_screen_size_real()
    fake_w, fake_h = get_screen_size_fake()
    zoom = w / fake_w
    print(f'zoom: {zoom}')
    return left*zoom, top*zoom, right*zoom, bottom*zoom

def task():
    points = []

    def on_click(x: int, y: int, button, pressed):
        hwnd = win32gui.GetForegroundWindow()  # 获取前台窗口句柄
        window_title = win32gui.GetWindowText(hwnd)  # 获取窗口的标题（名称）
        screen_w, screen_h = pyautogui.size()
        window_l, window_t, window_r, window_b = win32gui.GetWindowRect(hwnd)
        window_w = window_r - window_l
        window_h = window_b - window_t
        points.append({
            'windowname': window_title,
            'screensize': {
                'width': screen_w,
                'height': screen_h,
            },
            'globalposition': {
                'x': x, 'y': y
            },
            'windowposition': {
                'x': window_l, 'y': window_t
            },
            'windowsize': {
                'width': window_w,
                'height': window_h,
            }
        })
            
        return False
    
    mlistener = mouse.Listener(on_click=on_click)
    mlistener.start()
    mlistener.join()
    
    return json.dumps(points[0], ensure_ascii=False)